using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Windows.Forms;
using System.Windows.Forms.Design;
using System.Drawing.Design;
using LeisureCenter.Api;

namespace LeisureCenter.Controls
{
    [DesignTimeVisible(false)]
    public class FocusHandler : Component, IFocusHandler
    {
        private Control control;

        public Control Control
        {
            get { return control; }
            set
            {
                if (control != null)
                    control.KeyDown -= new KeyEventHandler(control_KeyDown);
                control = value;
                if (control != null)
                    control.KeyDown += new KeyEventHandler(control_KeyDown);
            } 
        }

        ~FocusHandler()
        {
            Control = null;
        }

        void control_KeyDown(object sender, KeyEventArgs e)
        {
            HandleControlKeyDown(this, e);
        }

        public static void HandleControlKeyDown(object sender, KeyEventArgs e)
        {
            if ((sender is IFocusHandler) && (HandleKey((IFocusHandler)sender, (IFocusHandler)sender, e.KeyCode)))
                e.Handled = true;
        }

        public static bool HandleKey(IFocusHandler handler, IFocusHandler source, Keys key)
        {
            IFocusHandler newHandler = null;
            switch (key)
            {
                case Keys.Up :
                    newHandler = handler.FocusUp;
                    break;
                case Keys.Down:
                    newHandler = handler.FocusDown;
                    break;
                case Keys.Left:
                    newHandler = handler.FocusLeft;
                    break;
                case Keys.Right:
                    newHandler = handler.FocusRight;
                    break;
            }
            if (newHandler != null)
                return newHandler.SetFocus(source, key);
            return false;
        }

        public static bool SetFocus(IFocusHandler handler, IFocusHandler source, Keys key)
        {
            if (source == handler)
            {
                return false;
            }
            else
                if (handler.Control != null && handler.Control.CanFocus)
                {
                    handler.Control.Focus();
                    return true;
                }
                else
                {
                    return HandleKey(handler, source, key);
                }
        }

        #region IFocusHandler Membres

        public bool SetFocus(IFocusHandler source, Keys key)
        {
            return SetFocus(this, source, key);
        }

        private IFocusHandler left;
        public IFocusHandler FocusLeft { get { return left; } set { if (value != this) left = value; } }

        private IFocusHandler right;
        public IFocusHandler FocusRight { get { return right; } set { if (value != this) right = value; } }

        private IFocusHandler up;
        public IFocusHandler FocusUp { get { return up; } set { if (value != this) up = value; } }

        private IFocusHandler down;
        public IFocusHandler FocusDown { get { return down; } set { if (value != this) down = value; } }

        #endregion
    }

    public class FocusManager : Component
    {
        private List<FocusHandler> handlers = new List<FocusHandler>();
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public List<FocusHandler> Handlers { get { return handlers; } }
    }
}
